﻿using ImperialWebSite.Presenters.Role.Classes;
using ImperialWebSite.Presenters.Role.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ImperialWebSite.Views.Main.Role
{
    public partial class QueryRoles : Page, IQueryRoles
    {
        private QueryRolesPresenter _presenter;
        private bool _successfulTransaction;

        public object RolesDataSource
        {
            set { gvRoles.DataSource = value; }
        }

        public string RoleNameText
        {
            get { return RoleName.Text; }
        }


        public object SessionCurrentRoles
        {
            get { return Session["currentRoles"]; }
            set { Session["currentRoles"] = value; }
        }

        public object SessionCurrentRole
        {
            get { return Session["currentRole"]; }
            set { Session["currentRole"] = value; }
        }


        public string TransactionMessage
        {
            get { return FailureText.Text; }
            set { FailureText.Text = value; }
        }

        public bool SuccessfulTransaction
        {
            get { return _successfulTransaction; }
            set { _successfulTransaction = value; }
        }


        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            _presenter = new QueryRolesPresenter(this);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitMessages();
                InitComponents();
            }
        }

        private void InitMessages()
        {
            SuccessfulTransaction = false;
            TransactionMessage = string.Empty;
        }

        private void InitComponents()
        {
            SessionCurrentRole = null;
            SessionCurrentRoles = null;
            LoadCurrentRoles();
            RoleName.Text = string.Empty;
            //btnRegister.Enabled = SessionCurrentRole != null;
        }

        private void LoadCurrentRoles()
        {
            _presenter.LoadCurrentRoles();
            gvRoles.DataBind();
        }


        protected void GvRoles_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvRoles.DataSource = SessionCurrentRoles;
            gvRoles.PageIndex = e.NewPageIndex;
            gvRoles.DataBind();
            SessionCurrentRole = null;
            RoleName.Text = string.Empty;
        }

        protected void GvRoles_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            GridView gv = sender as GridView;
            gv.SelectedIndex = int.Parse(e.CommandArgument.ToString());
            SessionCurrentRole = gv.SelectedDataKey["idRole"].ToString();
            TransactionMessage = string.Empty;
            RoleName.Text = gv.SelectedRow.Cells[0].Text;
            btnRegister.Enabled = SessionCurrentRole != null;
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (SessionCurrentRole != null)
            {
                _presenter.UpdateRole();
                if (SuccessfulTransaction)
                    InitComponents();
            }
            else
                TransactionMessage = "Please select a Role";
        }
    }
}